import record from './record';
import utils from './utils';
// console.log('breadcrumbs',breadcrumbs)
export default _ => {



    var _HTMLElement = window.HTMLElement || Element;
    var _Element = _HTMLElement.prototype;
    let createElement = document.createElement
    // document.createElement = function(type) {
        // console.log('createElement', type)
    //     if (type == 'img'||type == 'link') {
    //         console.log('createElement', type)
    //         let elem = createElement.apply(this, arguments);
    //         // let s =(+new Date())
    //         // elem.setAttribute('s',s)
    //         elem.onload = function() {
    //             let end = (+new Date())
    //               console.log('完成',elem)
    //               console.log('耗时',end-window.ssat)
    //         }
    //         return elem;
    //     }

    //     return createElement.apply(this, arguments)
    // }
    document.onclick = function(e) {
            if (e.target.onclick) {
                record.breadcrumbs.push({
                    type: 'onclick',
                    url:location.href,
                    time:utils.getTime(),                 
                    content: e.target.outerHTML
                })
            }
           
        }
        // var onclick = _Element.onclick;
        // console.log('onclick')
        // console.log(onclick)
        // _HTMLElement.onclick = function(params) {
        //     console.log('onclick')
        //     onclick.apply(this, arguments);
        // }
        // window.onclick= function(params) {
        //     console.log('sss',this)
        // }
    let addEventListener = _Element.addEventListener;
    let removeEventListener = _Element.removeEventListener;
    let cache = {};
    //要记录的事件
    let reportEvent = ['click']
    if (addEventListener) {
        _Element.addEventListener = function() {
            // console.log('addEventListener')
            addEventListener.apply(this, arguments);
            if (reportEvent.indexOf(arguments[0]) == -1) {
                // console.log(arguments[0])
                return false;
            }
            let name = 'fun' + (+new Date());
            cache[name] = function(params) {
                let type = arguments[0].type
                    // console.log(this.outerHTML)
                record.breadcrumbs.push({
                    type: type,
                    url:location.href,
                    time:utils.getTime(),
                    content: this.outerHTML
                })
            }
            arguments[1] = cache[name];
            this.setAttribute('godebug', name);
            addEventListener.apply(this, arguments);
        }
        _Element.removeEventListener = function(params) {

            removeEventListener.apply(this, arguments);
            if (reportEvent.indexOf(arguments[0]) == -1) {
                return false;
            }
            let name = this.getAttribute('godebug');
            arguments[1] = cache[name]
            removeEventListener.apply(this, arguments);
        }
    }
}
